import os
import platform
import re
import codecs  #编码
import collections  #收集模块
install = 'yum install -y '
remove = 'yum remove -y '
def yum_config():
    yum_bak = '/yum_bak/'
    yum_source = '/etc/yum.repos.d/'
    if os.path.isdir(yum_bak):
        cmd = 'cp -rf ' + yum_source + '*' + yum_bak
    else:
        os.remove(yum_bak)
        os.mkdir(yum_bak)
        cmd = 'mv ' + yum_source + '*' + yum_bak
    os.system(cmd)
    
def Get_OS_Info():
    catout = codecs.open("/etc/os-release","rb","gbk","ignore") #读取文件
    txt = catout.read() #获取文件内容

    ##获取版本号[7/8/9/10之类]
    os_version_id = re.compile(r'''VERSION_ID=\"\d*''') #定义正则表达式
    Txt_search = os_version_id.search(txt)    #开始使用正则匹配
    os_version_id = (Txt_search.group())
    version_id = str.replace(os_version_id, 'VERSION_ID=\"', ' ') #切割出版本号

    #获取系统类型[centos/debian....]
    os_type = re.compile(r'''PRETTY_NAME=\"\D*''')
    Txt_search = os_type.search(txt)
    types = (Txt_search.group())
    print(types)
    type_os = str.replace(types, 'PRETTY_NAME=', '')
    type_os = str.replace(type_os, '\"', '')
    print(type_os)

    print("系统版本: [ %s%s ]" %(type_os,version_id))

def network_chek():
    xt = platform.system()
    if xt == 'Windows':
        print("本机操作系统为Windows\n跳过网络检测")
    else:
        if os.system('ping 114.114.114.114 -c 4 > /dev/null') == 0:
            print("IPV4通讯正常")
            if os.system('ping www.baidu.com -c 4 > /dev/null') == 0:
                print("DNS通讯正常")
            else:
                exit("DNS 服务出错，请更换DNS服务器")
        else:
            exit("请检查网络")
print("检测网络连接...")

o = Get_OS_Info()